package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;


import java.time.LocalDate;
import java.util.List;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * excel文件上传
     * @param orderSettings
     */
    @Override
    public void uploadExcel(List<OrderSetting> orderSettings) {
        log.info("[预约设置-添加文件]date:{}",orderSettings);
        for (OrderSetting orderSetting : orderSettings) {
            orderSettingDao.insertExcel(orderSetting.getOrderDate(),orderSetting.getNumber());
       }
    }
    // 5 预约管理之当月预约信息展示
    @Override
    public List<OrderSetting> getOrderSettingByMonth(String year, String month) {
        log.info("预约管理之当月预约信息展示");
        //将String类型转成Integer
        Integer years = Integer.valueOf(year);
        Integer months = Integer.valueOf(month);
        LocalDate now = LocalDate.now();
        //本月初
        LocalDate monthFrist = now.of(years, months, 1);
        //本月末
        LocalDate monthend = monthFrist.plusMonths(1).minusDays(1);
        //DAO查询并返回
        return orderSettingDao.findByDate(monthFrist,monthend);
    }

    @Transactional
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        OrderSetting orderSetting1 =orderSettingDao.findCountByOrderDate((Date) orderSetting.getOrderDate());
        if (orderSetting1!=null){
        //当前日期进行了预约设置，修改操作
            if(orderSetting.getNumber()<orderSetting1.getReservations()){
                throw new BusinessRuntimeException(MessageConst.ORDERSETTING_FAIL);
            }
            orderSettingDao.editNumberByDate(orderSetting);
        }else{
        //当前日期没有进行预约设置，添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
